package gui;

import ge.escola.Serie;

import java.awt.Dimension;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.BorderFactory;
import javax.swing.JButton;

import javax.swing.WindowConstants;
import javax.swing.border.BevelBorder;
import javax.swing.border.SoftBevelBorder;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class SelecionaSerie extends javax.swing.JPanel {
	private JLabel jLabel1;
	private JTextArea textArea;
	private JLabel jLabel2;
	private JTextField caixaNome;
	private JButton buttonVoltar;
	private JButton buttonExcluir;
	private JButton buttonAcessar;
	
	private String nomeSerie;
	public static Serie serie;
	
	/**
	* Auto-generated main method to display this 
	* JPanel inside a new JFrame.
	*/
	
	public SelecionaSerie() {
		super();
		initGUI();
		
		atualizaSeries();
	}
	
	private void initGUI() {
		try {
			setPreferredSize(new Dimension(400, 300));
			this.setSize(800, 600);
			this.setBackground(new java.awt.Color(204,204,255));
			this.setLayout(null);
			{
				jLabel1 = new JLabel();
				this.add(jLabel1);
				jLabel1.setText("Seleciona serie");
				jLabel1.setBounds(220, 12, 330, 80);
				jLabel1.setForeground(new java.awt.Color(0,102,102));
				jLabel1.setFont(new java.awt.Font("Arial",0,48));
			}
			{
				textArea = new JTextArea();
				this.add(textArea);
				textArea.setBounds(62, 140, 380, 411);
				textArea.setBorder(new SoftBevelBorder(BevelBorder.LOWERED, null, null, null, null));
			}
			{
				jLabel2 = new JLabel();
				this.add(jLabel2);
				jLabel2.setText("Nome da serie");
				jLabel2.setBounds(550, 140, 144, 44);
				jLabel2.setForeground(new java.awt.Color(0,102,102));
				jLabel2.setFont(new java.awt.Font("Arial",0,22));
			}
			{
				caixaNome = new JTextField();
				this.add(caixaNome);
				caixaNome.setBounds(534, 183, 179, 36);
				caixaNome.setFont(new java.awt.Font("Arial",0,18));
				caixaNome.setForeground(new java.awt.Color(0,0,0));
				caixaNome.setHorizontalAlignment(SwingConstants.CENTER);
			}
			{
				buttonAcessar = new JButton();
				this.add(buttonAcessar);
				buttonAcessar.setText("ACESSAR");
				buttonAcessar.setBounds(485, 249, 102, 33);
				buttonAcessar.setFont(new java.awt.Font("Arial",0,14));
				buttonAcessar.setBackground(new java.awt.Color(255,255,255));
				buttonAcessar.addMouseListener(new MouseAdapter() {
					public void mouseClicked(MouseEvent evt) {
						buttonAcessarMouseClicked(evt);
					}
				});
			}
			{
				buttonExcluir = new JButton();
				this.add(buttonExcluir);
				buttonExcluir.setText("EXCLUIR");
				buttonExcluir.setBounds(660, 249, 102, 33);
				buttonExcluir.setFont(new java.awt.Font("Arial",0,14));
				buttonExcluir.setBackground(new java.awt.Color(255,255,255));
				buttonExcluir.addMouseListener(new MouseAdapter() {
					public void mouseClicked(MouseEvent evt) {
						buttonExcluirMouseClicked(evt);
					}
				});
			}
			{
				buttonVoltar = new JButton();
				this.add(buttonVoltar);
				buttonVoltar.setText("VOLTAR");
				buttonVoltar.setFont(new java.awt.Font("Arial",0,14));
				buttonVoltar.setBounds(660, 518, 102, 33);
				buttonVoltar.setBackground(new java.awt.Color(255,255,255));
				buttonVoltar.addMouseListener(new MouseAdapter() {
					public void mouseClicked(MouseEvent evt) {
						buttonVoltarMouseClicked(evt);
					}
				});
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private void buttonAcessarMouseClicked(MouseEvent evt) {
		nomeSerie = caixaNome.getText();
		capturaSerie();
		if(serie == null){
			JOptionPane.showMessageDialog(null, "Serie nao cadastrada.");
		}else{
			GerenciadorDeJanelas.alteraSerie = new AlteraSerie();
	        GerenciadorDeJanelas.window.setContentPane(GerenciadorDeJanelas.alteraSerie);
	        GerenciadorDeJanelas.window.setVisible(true);
			
		}
	}
	
	private void buttonExcluirMouseClicked(MouseEvent evt) {
		nomeSerie = caixaNome.getText();
		capturaSerie();
		if(serie != null){
			GerenciadorDeJanelas.todasSeries.remove(serie);
			JOptionPane.showMessageDialog(null, "Serie removida com sucesso!");
			
			String xml = GerenciadorDeJanelas.ARQUIVADOR.toXML(GerenciadorDeJanelas.todasSeries);
			GerenciadorDeJanelas.gravar(GerenciadorDeJanelas.arquivoDeSeries,xml);
			
			atualizaSeries();
			caixaNome.setText("");
		}else{
			JOptionPane.showMessageDialog(null, "Serie nao cadastrada.");
		}
	}
	
	private void buttonVoltarMouseClicked(MouseEvent evt) {
		GerenciadorDeJanelas.window.setContentPane(GerenciadorDeJanelas.menuAltera);
        GerenciadorDeJanelas.window.setVisible(true);
	}

	private void capturaSerie(){
		for(Serie serie2:GerenciadorDeJanelas.todasSeries){
			if(serie2.getNome().equals(nomeSerie)){
				serie = serie2;
			}
		}	
	}
	
	private void atualizaSeries(){
		String saida = "";
		if(GerenciadorDeJanelas.todasSeries.size()<=0){
			saida = "";
		}else{
			for(Serie serie:GerenciadorDeJanelas.todasSeries){
				saida+=serie.toString()+"\n";
			}
		}
		textArea.setEditable(true);
		textArea.setText(saida);
		textArea.setEditable(false);
	}
}
